package com.heima.article.service.impl;

import com.heima.model.article.entity.ApArticleContent;
import com.heima.article.dao.ApArticleContentDao;
import com.heima.article.service.ApArticleContentService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * 文章内容信息表(ApArticleContent)表服务实现类
 *
 * @author makejava
 * @since 2022-04-16 16:29:46
 */
@Service("apArticleContentService")
public class ApArticleContentServiceImpl implements ApArticleContentService {
    @Resource
    private ApArticleContentDao apArticleContentDao;

    /**
     * 通过ID查询单条数据
     *
     * @param articleId 主键
     * @return 实例对象
     */
    @Override
    public ApArticleContent queryById(Long articleId) {
        return this.apArticleContentDao.queryById(articleId);
    }

    /**
     * 分页查询
     *
     * @param apArticleContent 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<ApArticleContent> queryByPage(ApArticleContent apArticleContent, PageRequest pageRequest) {
        long total = this.apArticleContentDao.count(apArticleContent);
        return new PageImpl<>(this.apArticleContentDao.queryAllByLimit(apArticleContent, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param apArticleContent 实例对象
     * @return 实例对象
     */
    @Override
    public ApArticleContent insert(ApArticleContent apArticleContent) {
        this.apArticleContentDao.insert(apArticleContent);
        return apArticleContent;
    }

    /**
     * 修改数据
     *
     * @param apArticleContent 实例对象
     * @return 实例对象
     */
    @Override
    public ApArticleContent update(ApArticleContent apArticleContent) {
        this.apArticleContentDao.update(apArticleContent);
        return this.queryById(apArticleContent.getArticleId());
    }

    /**
     * 通过主键删除数据
     *
     * @param articleId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long articleId) {
        return this.apArticleContentDao.deleteById(articleId) > 0;
    }
}
